"""
切换iframe页面并筛选简历条件
"""
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

iframe_element = '//*[@id="recommendContent"]/div[2]/iframe'


def to_iframe(browser):
    """
    切换iframe
    :return:
    """
    try:
        driver = browser.find_element(By.XPATH, iframe_element)
        return driver
    except Exception as e:
        print(f"切换iframe时发生错误: {e}")
        raise e


def iframe_sift(driver):
    """
    iframe页面————筛选简历条件
    :return:
    """

    sift_element = '//*[@id="headerWrap"]/div/div/div[2]/div[1]'

    try:
        # 定位下拉框的触发元素
        sift = driver.find_element(By.XPATH, sift_element)
        sift.click()

        # # 定位下拉菜单中的选项
        # option = driver.find_element(By.XPATH, '//*[@id="headerWrap"]/div/div/div[2]/div[2]/ul')
        # # 点击选项
        # option.click()
        requirement_data = '''//*[@id="headerWrap"]/div/div/div[2]/div[2]/ul/li[text()=%s]''' % "全栈工程师 _ 厦门  2-3K"

        # 等待“数据”选项可见
        option_wu = WebDriverWait(driver, 5).until(
            EC.visibility_of_element_located((By.XPATH, requirement_data))
        )
        option_wu.click()

    except Exception as e:
        print(f"iframe页面筛选简历条件时发生错误: {e}")
        return False
